//
// Created by 张超 on 2024/5/6.
//
#include <string>
#include "HTFTSTraderSPI.h"
#include "HTFTSTraderAPI.h"
#include "HTFTSTraderAPIDataType.h"
#include "HTFTSTraderAPIStruct.h"
#include <atomic>
#include <map>
#include <mutex>
#ifndef TOPYTHONSO_TRADE_H
#define TOPYTHONSO_TRADE_H
using namespace HTFTS;
enum class ConnectStatus :int
{
    RECONNECTING = 1,
    CONNECTED = 2,
    END
};
class trade final : public HTFTS::HTFTSTraderSPI{
public:
    explicit trade(const std::string& FullConfigPath);
    void Start();
    void UserLogin(ReqUserLoginField* pField);
    void SingleOrderInsert(ReqSingleOrderInsertField *pField);
    //void setUser(const char* custid,const char* password);
    OrderField pOrderField_m{};
protected:
    void OnConnected() override;
    void RspQrySysTradeDateInfo(TradeDateInfo* pTradeDateInfo,  ErrorInfoField* pErrorInfo, RequestIDType reqId) override;
    void OnRspUserLogin(RspUserLoginField* userLoginRsp, ErrorInfoField* errorInfo, RequestIDType reqId) override;
    void OnRtnSubscribe(RspUser* pUser, ErrorInfoField * errorMsgRsp) override;
    void OnRspHolderNoField(RspUser* pUser, RspHolderNoField *pRspHolderNoField, RequestIDType reqId, bool isLast) override;
    void OnRtnOrder(RspUser* pUser, OrderField* pOrderField, ErrorInfoField* pErrorInfoField, RequestIDType reqId) override;
private:
    std::string sourceTerminalInfo;
    bool api_stat;
    HTFTS::HTFTSTraderAPI* traderAPI;
    HTFTS::ReqUser mUser{};
    std::atomic<ConnectStatus> mConnetSatus{};
    std::atomic<HTFTS::RequestIDType> mReqId{};
    std::atomic<HTFTS::OrderRefType> mOrderRef{};
    std::atomic<HTFTS::BatchIdType> mBatchId{};
    std::string m_CustomerId;
    std::string m_Password;

    static void PrintErrorInfoField(RspUser* pUser, ErrorInfoField* pErrorInfo, RequestIDType reqId);
    void PrintErrorInfoField(RspUser* pUser, ErrorInfoField* pErrorInfo);
    static void PrintErrorInfoField(ErrorInfoField* pErrorInfo, RequestIDType reqId);
    void PrintErrorInfoField(ErrorInfoField* pErrorInfo);
    static void PrintErrorInfoField(RspUser* pUser);


};


#endif //TOPYTHONSO_TRADE_H
